package com.blrs.service.project.sub.impI;


import java.sql.Timestamp;
import java.util.*;

import com.blrs.common.exception.ServiceException;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.common.utils.StringUtils;
import com.blrs.domain.ProjectStage;
import com.blrs.mapper.ProjectStageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.blrs.mapper.SubProjectInfoMapper;
import com.blrs.domain.SubProjectInfo;
import com.blrs.service.project.sub.ISubProjectInfoService;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-01
 */
@Service
public class SubProjectInfoServiceImpl implements ISubProjectInfoService 
{
    @Autowired
    private SubProjectInfoMapper subProjectInfoMapper;
    @Autowired
    private ProjectStageMapper projectStageMapper;
    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SubProjectInfo selectSubProjectInfoById(String id)
    {
        SubProjectInfo subProjectInfo = subProjectInfoMapper.selectSubProjectInfoById(id);
        List<ProjectStage> projectStages = projectStageMapper.selectProjectStageByProjectId(subProjectInfo.getId());
        subProjectInfo.setSubProjectStage(projectStages.get(0).getStageName());
        return subProjectInfo;
    }


    /**
     * 查询【请填写功能名称】列表
     * 
     * @param subProjectInfo 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SubProjectInfo> selectSubProjectInfoList(SubProjectInfo subProjectInfo)
    {
        List<SubProjectInfo> list = subProjectInfoMapper.selectSubProjectInfoList(subProjectInfo);
        // mergeSubProjectInfo(list);
        return list;
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param subProjectInfo 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSubProjectInfo(SubProjectInfo subProjectInfo)
    {
        //判断项目编号不能重复
        if (subProjectInfoMapper.selectSubProjectInfoByName(subProjectInfo.getProjectName(),subProjectInfo.getProjectId())>0){
            throw new ServiceException("子项目名称不能重复");
        }
        if (subProjectInfoMapper.selectSubProjectInfoByNumber(subProjectInfo.getProjectNumber(),subProjectInfo.getProjectId())>0){
            throw new ServiceException("子项目编号不能重复");
        }
        //判断该主项目下的子项目是否勾选了同样的项目阶段
        List<SubProjectInfo> subProjectInfos = subProjectInfoMapper.selectSubProjectByProjectId(subProjectInfo.getProjectId());
         if (subProjectInfos.size()>0){
             for (SubProjectInfo info : subProjectInfos){
                 List<ProjectStage> projectStages = projectStageMapper.selectProjectStageByProjectId(info.getId());
                 if (Objects.nonNull(projectStages.get(0).getStageName())){
                     int count=projectStageMapper.selectSubProjectInfoByProjectIdAndStage(info.getId(),subProjectInfo.getSubProjectStage());
                     if (count>0){
                         throw new ServiceException("该主项目下已有相同项目阶段的子项目，请勿重复添加");
                     }
                 }
             }
         }
        if(StringUtils.isNotEmpty(subProjectInfo.getSubProjectStage())){
            boolean flag = subProjectInfo.getSubProjectStage().contains("→");
            if(flag){
                String[] stages = subProjectInfo.getSubProjectStage().split("→");
                for (int i = 0; i < stages.length; i++) {
                    ProjectStage stage = new ProjectStage();
                    stage.setId(UUID.randomUUID().toString());
                    stage.setProjectId(subProjectInfo.getId());
                    stage.setStageName(subProjectInfo.getSubProjectStage());
                    stage.setName(stages[i]);
                    stage.setSort(i);
                    stage.setState("0");
                    projectStageMapper.insertProjectStage(stage);
                }
            }else{
                ProjectStage stage = new ProjectStage();
                stage.setId(UUID.randomUUID().toString());
                stage.setProjectId(subProjectInfo.getId());
                stage.setStageName(subProjectInfo.getSubProjectStage());
                stage.setName(subProjectInfo.getSubProjectStage());
                stage.setSort(0);
                stage.setState("0");
                projectStageMapper.insertProjectStage(stage);
            }
        }
        subProjectInfo.setState("0");
        subProjectInfo.setCreationId(SecurityUtils.getUserId().toString());
        subProjectInfo.setCreationName(SecurityUtils.getUsername());
        subProjectInfo.setCreationTime(new Timestamp(new Date().getTime()));
        return subProjectInfoMapper.insertSubProjectInfo(subProjectInfo);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param subProjectInfo 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSubProjectInfo(SubProjectInfo subProjectInfo)
    {
        return subProjectInfoMapper.updateSubProjectInfo(subProjectInfo);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSubProjectInfoByIds(String[] ids)
    {
        return subProjectInfoMapper.deleteSubProjectInfoByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSubProjectInfoById(String id)
    {
        return subProjectInfoMapper.deleteSubProjectInfoById(id);
    }

    @Override
    public int updateSubProjectInfoJob(SubProjectInfo subProjectInfo) {
        return subProjectInfoMapper.updateSubProjectInfo(subProjectInfo);
    }

    @Override
    public int completeSubProject(String id, String State) {
        return subProjectInfoMapper.completeSubProject(id,State);
    }

    @Override
    public List<SubProjectInfo> selectSubProjectByProjectId(String projectId) {
        return subProjectInfoMapper.selectSubProjectByProjectId(projectId);
    }

    private List<SubProjectInfo> mergeSubProjectInfo(List<SubProjectInfo> list) {
        Map<String, SubProjectInfo> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            String key = list.get(i).getProjectId()+list.get(i).getSubProjectStage()+list.get(i).getProductId();
            if (map.containsKey(key)) {
                list.get(i).setSubProjectStage(list.get(i).getSubProjectStage());
                map.put(key, list.get(i));
            } else {
                map.put(key, list.get(i));
            }
        }
        List<SubProjectInfo> subProjectInfos = new ArrayList<>();
        for (String key : map.keySet()) {
            subProjectInfos.add(map.get(key));
        }
        return subProjectInfos;
    }
}
